<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>欢迎页</title>
    <link rel="stylesheet" href="../../static/frame/layui/css/layui.css">
    <link rel="stylesheet" href="../../static/frame/static/css/style.css">
    <link rel="icon" href="../../static/frame/static/image/code.png">
</head>
<body class="body">

<div class="layui-row layui-col-space10 my-index-main">


    <form class="layui-form" action="" style="">
        <div class="layui-form-item">
            <label class="layui-form-label">搜索</label>
            <div class="layui-input-block">
                <input id="content" autocomplete="off" placeholder="请输入卡密" class="layui-input" type="text">
                <br>
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="search">搜索卡密</button>
                <hr class="layui-bg-blue">
            </div>

        </div>

    </form>

    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">生成数量</label>
            <div class="layui-input-block">
                <input type="text" name="num" required lay-verify="required" placeholder="生成卡密数量" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">卡密金额</label>
            <div class="layui-input-block">
                <input type="text" name="money" required lay-verify="required" placeholder="每张卡密所含有的金额"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="createkey">生成卡密</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                <hr class="layui-bg-blue">
            </div>

        </div>
    </form>
    <blockquote class="layui-elem-quote">注意：清空和删除卡密不管卡密是否已使用，一律不归还使用的余额！</blockquote>
    <div class="layui-form-item">
        <label class="layui-form-label">卡密操作</label>
        <div class="layui-input-block">
            <button class="layui-btn" onclick="Kami('ExportNoUse')">导出未使用卡密</button>
            <button class="layui-btn" onclick="Kami('ExportNoMoney')">导出零余额卡密</button>
            <button class="layui-btn" onclick="Kami('ExportAll')">导出全部卡密</button>
            <button class="layui-btn" onclick="Kami('DelNoMoney')">清空零余额卡密</button>
            <button class="layui-btn" onclick="Kami('DelNoUse')">清空未使用卡密</button>
            <button class="layui-btn" onclick="Kami('DelAll')">清空全部卡密</button>
        </div>
    </div>
    <table class="layui-hide" id="keylist" lay-filter="keylist"></table>
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="key">记录</a>
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
    </script>

</div>


<script src="../../static/jquery-3.3.1.js"></script>
<script type="text/javascript" src="../../static/frame/layui/layui.js"></script>
<script type="text/javascript" src="../../static/js/index.js"></script>
<script type="text/javascript" src="../../static/frame/echarts/echarts.min.js"></script>
<script type="text/javascript">
    layui.use(['form', 'table', 'element', 'layer', 'vip_tab'], function () {
        // 打开选项卡
        $('.my-nav-btn').on('click', function () {
            if ($(this).attr('data-href')) {
                vipTab.add($(this), '<i class="layui-icon">' + $(this).find("button").html() + '</i>' + $(this).find('p:last-child').html(), $(this).attr('data-href'));
            }
        });
        var table = layui.table, form = layui.form;
        window.layer = layui.layer;
        table.render({
            elem: '#keylist'
            , url: '../ajax.php?mod=getkeylist'
            , cellMinWidth: 130
            , cols: [[
                {field: 'ID', title: 'ID', width: 60}
                , {field: 'kami', title: '卡密'}
                , {field: 'creattime', title: '生成时间'}
                , {field: 'allmoney', title: '总金额'}
                , {field: 'lastmoney', title: '余额'}
                , {field: 'firstusetime', title: '初次使用'}
                , {field: 'lastusetime', title: '最后使用'}
                , {field: 'status', title: '状态'}
                , {toolbar: '#barDemo', title: '操作', width: 240}
            ]]
            , page: true
        });

        //监听性别操作
        form.on('switch', function (obj) {
            layer.load();
            $.ajax({
                url: '../ajax.php?mod=keystatus',
                type: 'POST',
                dataType: 'json',
                data: 'keyid=' + this.value + '&status=' + obj.elem.checked,
                success: function (data) {
                    layer.close(loading);
                    layer.alert(data.msg);

                },
                error: function (data) {
                    layer.close(loading);
                    layer.msg('['+data.status+']'+data.statusText);
                }
            });
        });

        table.on('tool', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {

                layer.confirm('真的删除行么', function (index) {
                    var loading = layer.load();
                    $.ajax({
                        url: '../ajax.php?mod=delkey',
                        type: 'POST',
                        dataType: 'json',
                        data: 'keyid=' + obj.data.ID,
                        success: function (data) {
                            layer.close(loading);
                            layer.alert(data.msg);
                            obj.del();
                        },
                        error: function (data) {
                            layer.close(loading);
                            layer.msg('['+data.status+']'+data.statusText);
                        }
                    });
                    layer.close(index);
                });
            } else if (obj.event === 'key') {
                var loading = layer.load();
                $.ajax({
                    url: '../ajax.php?mod=keylog',
                    type: 'POST',
                    dataType: 'json',
                    data: 'keyid=' + data.ID,
                    success: function (result) {
                        if (result.code === 1){
                            var tip = '';
                            for(var i=0;i<result.items.length;i++){
                                tip = tip + '['+result.items[i].time+'] '+result.items[i].msg+' （花费：'+result.items[i].spendmoney+'，余额'+result.items[i].lastmoney+'）<br>';
                            }
                            tip += '<br><br><br>只列出最近五十条记录，若想查看更多，请前往数据库执行这行语句查询：SELECT * FROM sq_log_kami WHERE keyid='+data.ID+'';
                            //layer.close(loading);
                            layer.open({
                                type: 1
                                ,title: false //不显示标题栏
                                , closeBtn: false
                                , area: '500px;'
                                , shade: 0.8
                                , id: 'keylog' //设定一个id，防止重复弹出
                                , resize: false
                                , btn: ['确定']
                                , btnAlign: 'c'
                                , moveType: 1 //拖拽模式，0或者1
                                , content: '<div style="padding: 30px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">' + tip + '</div>'
                            });
                        }else{
                            layer.msg(result.msg);
                        }
                        layer.close(loading);
                    },
                    error: function (data) {
                        layer.close(loading);
                        layer.msg('['+data.status+']'+data.statusText);
                    }
                })

                //layer.alert('编辑行：<br>'+ JSON.stringify(data))
            }
        });
        form.on('submit(search)', function (data) {
            //alert('111');
            table.reload('keylist', {
                url: '../ajax.php?mod=getkeylist'
                , where: {'search': $('#content').val()} //设定异步数据接口的额外参数
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        form.on('submit(createkey)', function (data) {
            var loading = layer.load();

            $.ajax({
                url: '../ajax.php?mod=creatkey',
                type: 'POST',
                dataType: 'json',
                data: 'sc_count=' + data.field.num + '&sc_money=' + data.field.money,
                success: function (data) {
                    layer.close(loading);
                    if (data.code === '1') {
                        table.reload('keylist', {
                            url: '../ajax.php?mod=getkeylist'
                        });
                        layer.confirm(data.keys, {
                            btn: ['下载卡密', '关闭'] //按钮
                        }, function () {
                            window.open('../ajax.php?mod=download&sign=' + data.sign);
                        });
                    } else {
                        layer.msg(data.msg);
                    }

                },
                error: function (data) {
                    layer.close(loading);
                    layer.msg('['+data.status+']'+data.statusText);
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

    });

    function Kami(operation) {
        layer.confirm('确定要执行操作：' + operation + '？', function () {
            var loading = layer.load();
            $.ajax({
                url: '../ajax.php?mod=KeyOperation',
                type: 'POST',
                dataType: 'json',
                data: 'operation=' + operation,
                success: function (data) {
                    if (data.code === 1){
                        layer.alert('您的卡密已就绪，<a href="../ajax.php?mod=download&sign='+data.sign+'">点击这里下载卡密</a>');
                    }else if (data.code === 2){
                        layer.alert('操作成功，总共影响'+data.nums+'行数据');
                    }else{
                        layer.alert(data.msg);
                    }
                    layer.close(loading);
                },
                error: function (data) {
                    layer.msg('['+data.status+']'+data.statusText);
                    layer.close(loading);
                }
            });
        });

    }
</script>
</body>
</html>